package com.ydj.framework.mysql;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@AllArgsConstructor
@Accessors(chain = true)
public class PageArgs {
    private Long page;

    private Long pageSize;

    private Long totalSize;

    private Long totalPage;

    public PageArgs(long page, long pageSize) {
        this.page = page;
        this.pageSize = pageSize;
    }

    /**
     * 注意，调用这个函数。需要已经设置好 totalSize, page, pageSize 才能用
     */
    public PageArgs autoCalTotalSize() {
        this.totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
        return this;
    }

    public <T> IPage<T> toIPage() {
        return new Page<>(page, pageSize);
    }

    public static PageArgs fromIPage(IPage<?> ipage) {
        return new PageArgs(ipage.getCurrent(), ipage.getSize())
                .setTotalSize(ipage.getTotal())
                .setTotalPage(ipage.getPages());
    }
}